var myApp = new Framework7({
	modalTitle: false, //
	animateNavBackIcon: false,
	swipeBackPage: true,
	swipeBackPageAnimateShadow: false, //关闭 滑动返回时候的 box-shadow 动画
	swipeBackPageAnimateOpacity: false, //半透明效果关闭
	sortable: false, //关闭可排序列表
	//animatePages:false, //禁止动画
	//hideNavbarOnPageScroll:true,//自动隐藏导航栏
	cache: false,
	domCache: false,
	modalButtonOk: "确认",
	modalButtonCancel: "取消",
	modalPreloaderTitle: "加载中...",
	smartSelectBackText: '返回',
	smartSelectPopupCloseText: '关闭',
	smartSelectPickerCloseText: '取消',
	notificationCloseButtonText: '关闭',
	showBarsOnPageScrollEnd: false,
	showBarsOnPageScrollTop: false,
	scrollTopOnNavbarClick: true, //点击中间滚回顶部
	hideTabbarOnPageScroll: false,
	hideToolbarOnPageScroll: false,
	hideNavbarOnPageScroll: false,
	preroute: function(view, options) {},
	onAjaxStart: function(xhr) {
		//myApp.showIndicator();
	},
	onAjaxComplete: function(xhr) {
		//myApp.hideIndicator();
	},
	onAjaxError: function(xhr) {
		myApp.alert("网络异常,加载失败");
	},
	onPageInit: function(app, page) {
		//scrollInit(page);
	}
});

var $$ = Dom7;

/*
var mainView = myApp.addView('.view-main', {
    dynamicNavbar: true
});
*/

var view1 = myApp.addView('#view-1', {
	dynamicNavbar: true,
	domCache: false,
});
var view2 = myApp.addView('#view-2', {
	dynamicNavbar: true,
	domCache: false,
});
var view3 = myApp.addView('#view-3', {
	dynamicNavbar: true,
	domCache: false,
});
var view4 = myApp.addView('#view-4', {
	dynamicNavbar: true,
	domCache: false,
});
var view5 = myApp.addView('#view-5', {
	dynamicNavbar: true,
	domCache: false,
});
myApp.onPageInit('about', function(page) {
	$$('.create-page').on('click', function() {
		createContentPage();
	});
});
//var mainView = myApp.addView('.view-main');
myApp.onPageInit('*', function(page) {
	$$('.toolbar').addClass('toolber-hide');
});
/*myApp.onPageAfterBack('*',function (page) {
    var activename = '#' + page.view.activePage.name;
    if(activename === page.view.selector){
        $$('.toolbar').removeClass('toolber-hide');
    }
});*/
myApp.onPageBack('*', function(page) {
	if($$('.toolber-hide')) {
		$$('.toolbar').removeClass('toolber-hide');
	}
});
/*var mainView = myApp.addView('.view-main');
function  scrollInit(page){
    mainView.hideToolbar();
}*/

var dynamicPageIndex = 0;

function reloadApp() {
	window.location.href = "/";
}

function reloadnowpage() {
	myApp.showIndicator();
	view2.router.refreshPage();
	myApp.hideIndicator();
}

function curl_get(url, callback) {
	myApp.showIndicator();
	var result = $$.ajax({
		method: "GET",
		url: url,
		timeout: 10000,
		success: function(data) {
			/*var coke = getCookie('username');*/
			if(data == "notlogin") {
				window.location.href = "user.php?act=login";
				return;
			}
			myApp.hideIndicator();
			callback(data);
			$$('img.lazy').trigger('lazy');
		},
		error: function(data) {
			myApp.hideIndicator();
		},
		complete: function(data) {
			myApp.hideIndicator();
		}
	});
	return result;
}

function curl_post(url, param, callback) {
	//myApp.showIndicator();
	var result = $$.ajax({
		method: "POST",
		url: url,
		data: param,
		timeout: 10000,
		success: function(data) {
			/*var coke = getCookie('username');
			 if(!coke || coke < 1){
			 window.location.href = "user.php?act=login";
			 }*/
			if(data == "notlogin") {
				window.location.href = "user.php?act=login";
				return;
			}
			callback(data);
			myApp.hideIndicator();

		},
		error: function(data) {
			myApp.hideIndicator();
		},
		complete: function(data) {
			myApp.hideIndicator();
		}
	});
	return result;
}

var u = {
	//登陆
	login: function(a) {
		var data = myApp.formToJSON($$(a).parents("form"));
		var url = $$(a).parents("form").attr("action");
		curl_post(url, data, function(result) {
			var results = JSON.parse(result);
			if(results.code == 0) {
				my_toast('登陆成功');
				myApp.closeModal();
				window.location.href = "/";
			} else {
				my_toast(results.message);
			}
		});
	},
	//退出
	logout: function() {
		curl_get(ROOT + "/user/logout", function(r) {
			myApp.loginScreen();
			my_toast("成功退出");

		})
	}
}

// 创建并显示新窗口
function openWebview() {
	var w = plus.webview.open("test.html", 'test.html', {
		scrollIndicator: 'none',
		scalable: false,
		popGesture: 'none'
	}, 'zoom-out');
	//var w = plus.webview.create( "test.html" );
	//plus.webview.show( w ); // 显示窗口
}
var ylistallarr = new Array();
var huanqu = {
	ylistallhtml: "",
	addselect: function() {
		myApp.smartSelectAddOption('#pidd select', '<option value="apple" selected>Apple</option><option value="apple" selected>Apple</option><option value="apple" selected>Apple</option>');
	},
	//获取区最原始的html
	getquhtml: function() {
		var listall = $$("#shqusho").find("input");
		var ylistallhtml = '';
		if(localStorage.getItem('ylistallhtml')) {
			ylistallhtml = localStorage.getItem('ylistallhtml');
		} else {
			listall.each(function(a, b) {
				ylistallarr[$$(this).val()] = $$(this).attr('data-xm');
				ylistallhtml += '<li>' +
					'<label class="label-checkbox item-content" style="margin-left:5px;padding-left: 5px;">' +
					'<input type="checkbox" name="quhao" data-xm="' + $$(this).attr('data-xm') + '" value="' + $$(this).val() + '">' +
					'<div class="item-media">' +
					'<i class="icon icon-form-checkbox"></i>' +
					'</div>' +
					'<div class="item-inner" style="margin-left:5px;padding: 0px;">' +
					'<div class="item-title">' + $$(this).val() + '(' + $$(this).attr('data-xm') + ')</div>' +
					'</div>' +
					'</label>' +
					'</li>';
			})
			localStorage.setItem("ylistallhtml", ylistallhtml);
		}

		return ylistallhtml;
	},

	//
	getquoption: function() {
		var listall = $$("#shqusho").find("input");
		var option = ''; //合并后的选区
		if(localStorage.getItem('option')) {
			option = localStorage.getItem('option');
		} else {
			listall.each(function(a, b) {
				option += '<option value="' + $$(this).val() + '">' + $$(this).val() + '(' + $$(this).attr('data-xm') + ')</option>';
			});
			localStorage.setItem("option", option);
		}
		return option;
	},

	//合并
	hebing: function() {
		var listval = "";
		var option = this.getquoption();
		var list = $$("#shqusho").find("input:checked");
		if(list.length < 1) {
			myApp.alert("你未选择");
			return;
		}
		this.getquhtml();
		$$("#setp").hide();
		list.each(function() {
			if(listval == "") {
				listval = $$(this).val();
			} else {
				listval = listval + "," + $$(this).val();
			}
			$$(this).parents('li').remove();
		});
		var htmls = '<li>' +
			'<a href="#" id="pidd"  class="item-link smart-select" data-page-title="选择交换的区" data-back-text=" " >' +
			'<select name="' + listval + '" multiple>' +
			'<optgroup>' + option + '</optgroup>' +
			'</select>' +
			'<div class="item-content">' +
			'<div class="item-inner">' +
			'<div class="item-title">' + listval + '</div>' +
			'<div class="item-after smart-select-value"></div>' +
			'</div>' +
			'</div>' +
			'</a>' +
			'</li>';
		$$("#huangqu-list").append(htmls);
	},
	nohebing: function() {
		var listall = $$("#shqusho").find("input");
		var option = this.getquoption();
		this.getquhtml();
		$$("#setp").hide();
		listall.each(function() {
			var htmls = '<li>' +
				'<a href="#"  class="item-link smart-select" data-page-title="选择交换的区" data-back-text=" " >' +
				'<select name="' + $$(this).val() + '" multiple>' +
				'<optgroup>' + option + '</optgroup>' +
				'</select>' +
				'<div class="item-content">' +
				'<div class="item-inner">' +
				'<div class="item-title">' + $$(this).val() + '</div>' +
				'<div class="item-after smart-select-value"></div>' +
				'</div>' +
				'</div>' +
				'</a>' +
				'</li>';
			$$("#huangqu-list").append(htmls);
		});
		$$("#shqusho").html('');
	},
	save: function() {
		var formData = myApp.formToJSON('#hequlist');
		var url = $$('#hequlist').attr('action');
		if(JSON.stringify(formData).length <= 2) {
			myApp.alert("你没有做任何操作!");
			return;
		}
		for(var i in formData) {
			if(JSON.stringify(formData[i]).length <= 2) {
				myApp.alert("你还有换区没有设置好!");
				return;
			}
		}
		console.log(formData);
		curl_post(url, formData, function() {

		});
	},
	cancel: function() {
		var strlength = $$("#huangqu-list").html();
		if(strlength.length != 0) {
			$$("#shqusho").html('');
			$$("#shqusho").html(this.getquhtml());
			$$("#huangqu-list").html('');
			$$("#setp").show();

			localStorage.setItem('ylistallhtml', '');
			localStorage.setItem('option', '');
		}
	}
}

function clicked_qrcodepay(id, s, t) {
	if($$('#sum')) {
		var sum = $$('#sum').val();
		if(sum == '') {
			plus.nativeUI.alert('请输入金额');
			return;
		}
		if(!chk(sum)){
			plus.nativeUI.alert('金额是数字且最多二位小数');
			return;
		}
	}
	clicked(id, s, t);
}
//当面付
var weburl = "https://isv.tiaze.com/";
function dmpay(type, result) {
	var sum = $$('#sum').val();
/*	if(!verify_codepay(result)) {
		plus.nativeUI.alert('不是付款码,请查证后重试');
		return;
	};*/
	var model = "api/merchant/login";
	  var  model1 = "api/merchant/MOrder";
	  var  model2 = "api/merchant/MoneyPay";
	  var model3 = "api/merchant/TradeQuery";
	  var  model4 = "api/merchant/TradePay";
	    if(!localStorage.getItem('token')){
	    		curl_post(weburl+ model,{phone:"15058132455",password:"718000"},function(data){
		var datas =  JSON.parse(data);
		localStorage.setItem('token', datas.token); //获取token
	})
	    }

	
		/*curl_post(weburl+ model2,{type:"all",token:localStorage.getItem('token')},function(data){
		//var data =  JSON.parse(data);
		console.log("11111111");
		console.log(data);
	});*/
	var times = Date.parse(new Date());
	    times = times / 1000 ;
	    
	var datas = {
		code:result,
		token:localStorage.getItem('token'),
		total_amount:'0.1',
		out_trade_no:'45678908'+times
	}
	curl_post(weburl+ model4,datas,function(data){
		if(!data || data == ''){
			plus.nativeUI.alert('请求失败');
			return;
		}
		var datas =  JSON.parse(data);
		//console.log(unescape(data.replace(/\\u/g, '%u')));
		console.log(data);
		console.log(datas.msg);
	});
	
}

//验证是不是付款码
function verify_codepay(result) {
	var numarr = [10, 11, 12, 13, 14, 15, 18, 28, 31];
	if(!checkNumber(result)) {
		return false;
	}
	console.log("通过");
	var str = result.substring(0, 2); //取得前2位
	console.log(str);
	if(contains(numarr,str)) {
		return true;
	} else {
		return false;
	}
}

//验证字符串是否是数字
function checkNumber(theObj) {
	var reg = /^[0-9]*$/;
	if(reg.exec(theObj)) {
		return true;
	}
	return false;
}
/**
 * 某元款是否存在于数组中
 * @param {Object} arr 数组
 * @param {Object} obj 元款
 */
function contains(arr, obj) {
	var i = arr.length;
	while(i--) {
		if(arr[i] == obj) {
			return true;
		}
	}
	return false;
}

/**
 * 验证是不是整数或者小数
 * @param {Object} res
 */
function chk(res) {
	var patrn = /^[0-9]+([.]{1}[0-9]{1,2})?$/;
	if(patrn.exec(res)) {
	   return true;
	}
	return false;
}